package com.freecloud.bitorrent.config;

import com.freecloud.bitorrent.domain.server.codec.BitTorrentDecoder;
import com.freecloud.bitorrent.domain.server.handler.DHTServerHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.DatagramChannel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class DHTChannelInitializer extends ChannelInitializer<DatagramChannel> {

	private final DHTServerHandler dhtServerHandler;
	private final BitTorrentDecoder bitTorrentDecoder;

	public DHTChannelInitializer(DHTServerHandler dhtServerHandler, BitTorrentDecoder bitTorrentDecoder) {
		this.dhtServerHandler = dhtServerHandler;
		this.bitTorrentDecoder = bitTorrentDecoder;
	}

	@Override
	protected void initChannel(DatagramChannel ch) throws Exception {
		ChannelPipeline pipeline = ch.pipeline();
		log.info("DHTChannelInitializer ： {}", ch);
		//解码器
		pipeline.addLast(bitTorrentDecoder);
		pipeline.addLast(dhtServerHandler);
	}
}
